package com.huawei123.hasee.cniao5store.mvp.view.fragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.base.BaseFragment;
import com.huawei123.hasee.cniao5store.bean.MyGvBean;
import com.huawei123.hasee.cniao5store.utils.UIUtils;
import com.huawei123.hasee.cniao5store.view.LoadingPager;
import com.huawei123.hasee.cniao5store.view.widget.MyEnterLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：我的界面
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 下午 4:59
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 下午 4:59
 * 修改备注：
 */

public class MyFragment extends BaseFragment {

    @BindView(R.id.gv_title_grid)
    GridView gv_title_grid;
    @BindView(R.id.book_game_layout)
    MyEnterLayout book_game_layout;
    @BindView(R.id.buy_layout)
    MyEnterLayout buy_layout;
    @BindView(R.id.huaban_layout)
    MyEnterLayout huaban_layout;
    @BindView(R.id.setting_computer)
    MyEnterLayout setting_computer;
    @BindView(R.id.faq_layout)
    MyEnterLayout faq_layout;
    @BindView(R.id.check_update_layout)
    MyEnterLayout check_update_layout;
    @BindView(R.id.about_layout)
    MyEnterLayout about_layout;

    private List<MyGvBean> gvBeanList = new ArrayList<>();

    @Override
    protected void loading() {
        setState(LoadingPager.LoadResult.success);
        //        new Thread(new Runnable() {
        //            @Override
        //            public void run() {
        //                SystemClock.sleep(2000);
        //                setState(LoadingPager.LoadResult.success);
        //            }
        //        }).start();
    }

    @Override
    protected View createSuccessView() {
        View view = UIUtils.inflate(R.layout.fragment_my);
        ButterKnife.bind(this, view);

        gvBeanList.clear();
        /*四按钮*/
        gvBeanList.add(new MyGvBean(UIUtils.getString(R.string.market_prize), R.drawable.icon_market_lucky_draw));
        gvBeanList.add(new MyGvBean(UIUtils.getString(R.string.market_gift), R.drawable.ic_mine_package_normal));
        gvBeanList.add(new MyGvBean(UIUtils.getString(R.string.appzone_comments), R.drawable.icon_market_comment));
        gvBeanList.add(new MyGvBean(UIUtils.getString(R.string.market_mine_message), R.drawable.icon_market_message));
        MyAdapter myAdapter = new MyAdapter(getContext(), gvBeanList);
        gv_title_grid.setNumColumns(gvBeanList.size());
        gv_title_grid.setAdapter(myAdapter);
        /*下部条目*/
        book_game_layout.setTitle(UIUtils.getString(R.string.reserve_warpup_game_str));
        buy_layout.setTitle(UIUtils.getString(R.string.purchase_title));
        huaban_layout.setTitle(UIUtils.getString(R.string.mine_point_area));
        setting_computer.setTitle(UIUtils.getString(R.string.action_settings));
        faq_layout.setTitle(UIUtils.getString(R.string.menu_feedback));
        check_update_layout.setTitle(UIUtils.getString(R.string.settings_check_version_update));
        about_layout.setTitle(UIUtils.getString(R.string.about));
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        gvBeanList.clear();
    }

    class MyAdapter extends BaseAdapter {


        private Context context;
        private List<MyGvBean> myGvBeans;

        public MyAdapter(Context context, List<MyGvBean> gvBeanList) {
            this.context = context;
            this.myGvBeans = gvBeanList;
        }


        @Override
        public int getCount() {
            return myGvBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return myGvBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyGvBean myGvBean = myGvBeans.get(position);
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.appdetail_subcat_title, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.mTV_ItemTitle.setText(myGvBean.getName());
            Glide.with(context).load(myGvBean.getIconId()).into(viewHolder.mIV_appicon);
            return convertView;
        }

        class ViewHolder {

            ImageView mIV_appicon;
            TextView mTV_ItemTitle;

            public ViewHolder(View view) {
                mIV_appicon = (ImageView) view.findViewById(R.id.mIV_appicon);
                mTV_ItemTitle = (TextView) view.findViewById(R.id.mTV_ItemTitle);
            }
        }
    }
}
